package durst.action.account.include;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;

import com.sdicons.json.mapper.JSONMapper;
import com.sdicons.json.mapper.MapperException;

import durst.action.CalendarBasicAction;
import durst.data.Event;
import durst.data.Event.Range;
import durst.data.Invite;
import durst.data.InvitedEvent;
import durst.data.UserCalendar;
import durst.data.dao.DaoFactory;

@UrlBinding("/action/include/events/{$event}")
public class EventsIncludeAction extends CalendarBasicAction {
	@DefaultHandler
	public Resolution include() throws MapperException, SQLException {
		Set<Event> events = new LinkedHashSet<Event>();

		Calendar now = Calendar.getInstance();
		now.add(Calendar.DATE, -7);
		now.set(Calendar.HOUR_OF_DAY, 0);
		now.set(Calendar.MINUTE, 0);
		now.set(Calendar.SECOND, 0);
		now.set(Calendar.MILLISECOND, 0);
		
		Calendar future = Calendar.getInstance();
		future.add(Calendar.YEAR, 1);

		for (UserCalendar cal : context.getAccount().getCalendars()) {
			if (cal.getEvents() == null)
				DaoFactory.getInstance().createDao(UserCalendar.class).refresh(cal);
			List<Event> list = new ArrayList<Event>(cal.getEvents());
			events.addAll(Event.eventsInRange(list, now, future));

			for (Event e : list)
				if (!events.contains(e)
						&& e.isRepeated()
						&& (e.getDoNotRepeatPast().after(now.getTime()) || e.getDoNotRepeatPast()
								.before(future.getTime())))
					events.add(e);
		}
		context.getRequest().setAttribute("eventCount", events.size());
		context.getRequest().setAttribute("events", JSONMapper.toJSON(events).render(false));
		context.getRequest().setAttribute("columns", "['id','title','protoStartTime','shortDesc','repeated']");
		context.getRequest().setAttribute("colTitles", new String[] { "ID", "Title", "Starts", "Description", "Repeats"});
		context.getRequest().setAttribute("style", new String[] { "width:50px;", "width:100px;", "width:200px;", "width:200px;", "width:50px;"});
		return new ForwardResolution("/account/includes/events.jsp");
	}
	
	public Resolution invited() throws SQLException, MapperException {
		Set<InvitedEvent> events = new LinkedHashSet<InvitedEvent>();
		Collection<Invite> invites = context.getAccount().getInvitedTo();
		
		Calendar now = Calendar.getInstance();
		Calendar future = Calendar.getInstance();
		future.add(Calendar.YEAR, 1);
		
		for (Invite invite : invites) {
			if (invite.getEvent().getDescription() == null)
				DaoFactory.getInstance().createDao(Event.class).refresh(invite.getEvent());

			if (invite.getEvent().eventInRange(now, future) == Range.INSIDE ||
					invite.getEvent().isRepeated() && (invite.getEvent().getDoNotRepeatPast().after(now.getTime()) ||
							invite.getEvent().getDoNotRepeatPast().before(future.getTime())))
				events.add(new InvitedEvent(invite.getEvent(), invite.isJumplist()));
		}
		
		context.getRequest().setAttribute("eventCount", events.size());
		context.getRequest().setAttribute("events", JSONMapper.toJSON(events).render(false));
		context.getRequest().setAttribute("columns", "['id','title','protoStartTime','shortDesc','repeated', 'jumplist']");
		context.getRequest().setAttribute("colTitles", new String[] { "ID", "Title", "Starts", "Description", "Repeats", "Jumplisted"});
		context.getRequest().setAttribute("style", new String[] { "width:50px;", "width:150px;", "width:200px;", "width:200px;", "width:50px;", "width:50px;"});
		return new ForwardResolution("/account/includes/events.jsp");
	}
}
